﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LastfmNet
{
    internal static class XmlNodeExtensions
    {
        public static string GetAttributeValue(this XmlNode node, string attributeName)
        {
            if (node.Attributes[attributeName] == null) return string.Empty;
            else return node.Attributes[attributeName].Value;
        }

        public static string GetChildValue(this XmlNode node, string childName)
        {
            if (node == null) return string.Empty;
            if (node[childName] != null) return node[childName].InnerText;
            else return string.Empty;
        }

        public static List<string> GetChildValuesList(this XmlNode node, string childrenNames)
        {
            if (node == null) return new List<string>();
            List<string> valuesList = new List<string>();
            foreach (XmlNode item in node.ChildNodes)
            {
                if (item.Name == childrenNames)
                {
                    valuesList.Add(item.InnerText);
                }
            }
            return valuesList;
        }

        public static string GetChildValueWithSpecialAtrribute(this XmlNode node, string name, string attributeName, string attributeValue)
        {
            if (node == null) return string.Empty;
            if (node[name] != null)
            {
                XmlNodeList xmlNodeList = node.ChildNodes;
                foreach (XmlNode item in xmlNodeList)
                {
                    if (item.Name == name && item.Attributes[attributeName].Value == attributeValue)
                    {
                        return item.InnerText;
                    }
                }
                return string.Empty;
            }
            else return string.Empty;
        }

        public static XmlNode GetChild(this XmlNode node, string name)
        {
            if (node == null) return null;
            if (node[name] != null) return node[name];
            else return null;
        }
    }
}
